package containerlogistik.implementation;

import static containerlogistik.implementation.Values.*;
import containerlogistik.interfaces.Length;

public class LengthInM extends AbstractValue<Length> implements Length {

    private double meter;

    private LengthInM(double meter) {
        this.meter = meter;
    }

    static LengthInM valueOf(double meter) {
        return new LengthInM(meter);
    }

    @Override
    public double m() {
        return this.meter;
    }

    @Override
    public double km() {
        return this.m() * M_IN_KM;
    }

    @Override
    public double circumference() {
        return this.m() * RADIUS_AS_CIRCUMFERENCE;
    }

    @Override
    public boolean isZero() {
        return (this.equals(ZERO_LENGTH));
    }

    @Override
    public boolean isValid() {
        return !(Double.isNaN(this.meter) || Double.isInfinite(this.meter));
    }

    @Override
    public String toString() {
        return String.format("LengthInM{kilometer=%.2f, meter=%.2f}",
                this.km(), this.m());
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) return true;
        if (!(obj instanceof Length)) return false;
        return this.compareTo((Length)obj) == 0;
    }

    @Override
    public int hashCode() {
        return hashDouble(this.m());
    }

    @Override
    public int compareTo(Length o) {
        return Double.compare(this.m(), o.m());
    }

    @Override
    public Length add(Length value) {
        return lengthInM(this.m() + value.m());
    }

    @Override
    public Length sub(Length value) {
        return lengthInM(this.m() - value.m());
    }

    @Override
    public Length mul(double factor) {
        return lengthInM(this.m() * factor);
    }

    @Override
    public Length div(double factor) {
        return lengthInM(this.m() / factor);
    }

    @Override
    public Length inverse() {
        return lengthInM(-this.m());
    }

    @Override
    public double div(Length value) {
        return (this.m() / value.m());
    }
}